<?php

class p_list extends page_controllor 
{
    public function __construct($get_param)
    {
    	$this->set_page_id('p_product_list');
        parent::__construct($get_param);
    }
    
    public function run()
    {
    	$is_multi = 0;
		//加载分类列表
        $oArraytree = new lib_arraytree(array());
        $oArraytree->restart(k::config('product_class'));
        $namefield = $this->__isLangEng ? 'eng_name' : 'name';
        $out_data['product_class'] = $oArraytree->get_id2field( $namefield );
        
        
        //根据
        list($map_id , $class_id , $material_id) = explode(',' , $this->get_input(0));
        
        //最终页
        $str2show = $this->get_input(1).'/'.$this->get_input(2).'/'.$this->get_input(3);
        
        //是否支持预订
        $is_multi = $this->get_input(1) == 1 ? 1 : 0;
        if($this->visitor['level'] != TL_USER_LEVEL_VIP)
        {
            $is_multi = 0;
        }
        //日期
        $date = $this->get_input(2);
        
        //根据条件取子分类
        $map_id = (int)$map_id;
        if($map_id)
        {
            $oArraytree->restart(k::config('product_map'));
            $aMapIds = $oArraytree->get_children_id($map_id);
            $aMapIds[] = $map_id;
        }
        $material_id = (int)$material_id;
        if($material_id)
        {
            $oArraytree->restart(k::config('material'));
            $aMaterialIds = $oArraytree->get_children_id($material_id);
            $aMaterialIds[] = $material_id;
        }
        $class_id = (int)$class_id;
        if($class_id)
        {
            $oArraytree->restart(k::config('product_class'));
            $aClassIds = $oArraytree->get_children_id($class_id);
            $aClassIds[] = $class_id;
        }
        
        $page = $this->get_input(3);
            $page = $page>0 ? $page : 1;
        $type_code = $map_id.','.$class_id.','.$material_id;
        $page_url = '/product/list/'.$type_code.'/0/0/{page}';
        //产品列表
        $oProduct = K::load_mod('product');
        
        $condition = 'stat='.K_COMMON_YES;
        $condition .= ' AND is_multi='.$is_multi;
        $condition .= $date ? ' AND cdate="'.$date.'"' : '';
        $condition .= $aMapIds ? ' AND map_id in('.implode(',' , $aMapIds).')' : '';
        $condition .= $aMaterialIds ? ' AND material_id in('.implode(',' , $aMaterialIds).')' : '';
        $condition .= $aClassIds ? ' AND class_id in('.implode(',' , $aClassIds).')' : '';
        
        $oProduct->list_product($total , $page , 9 , $condition) or $this->busy();
        $aProduct = $oProduct->get_data();
        
        $aPid = array();
        if(count($aProduct) > 0)
        {
            foreach ($aProduct as $k => $row)
            {
                $aProduct[$k]['name'] = $this->__isLangEng ? K_str::un_html($row['eng_name']) : K_str::un_html($row['name']);
                $aProduct[$k]['cover_url'] = product2coverpath($row['id'] , true);
                $aProduct[$k]['product_url'] = '/product/show/'.$row['sn'].'/'.$str2show;
                
                $aPid[] = $row['id'];
            }
        }
        
        
        if(count($aPid))
        {
	        $oBuy = K::load_mod('product_buy');
	        $oBuy->check_by_uid_pid($this->visitor['uid'] , $aPid) or $this->busy();
	        $aOrder = $oBuy->get_data();
        }
        
        $out_data['products'] = $aProduct;
        $out_data['orders'] = $aOrder;
        $out_data['map_id'] = $map_id;
        $out_data['material_id'] = $material_id;
        $out_data['class_id'] = $class_id;
        $out_data['is_multi'] = $is_multi;
        $out_data['total'] = $total;
        $out_data['page'] = $page;
        $out_data['page_url'] = $page_url;
        $out_data['date'] = $date;
        
        
        $this->set_page_title($this->__lang['gTitle_p_productList'].'_'.$this->__lang['gTitle_page_title']);
        $this->output('productlist',$out_data);
    }
}
?>